<?php
# @Author: fanrehai
# @Date:   2019-11-25 16:32:44
# @Filename: Appadv.php
# @Last modified by:   fanrehai
# @Last modified time: 2019-11-25 17:38:41

/**
 * wangjianbao
 */
namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

/**
 * 广告模型
 */
class Appadvlist extends Model
{
	// protected $table = "ds_appadv";
	protected $table = "dsc_app_ad_list";

	public static function getListsArray($ap_id,$area_id=0, $page=1, $page_size=5){
		$data = self::all(function($query) use($ap_id,$area_id,$page,$page_size){
			$time = time();
			$query->field('id,link,url,target_type,content,link_id');
			$query->where('position_id',$ap_id);
			$query->where('enabled',1);
			$query->where('start_time','<',$time);
			$query->where('end_time','>',$time);
			$query->order('orderby asc,id desc');
			if($area_id){
				$area_list=getarea($area_id);
				$query->where('area_id','in',$area_list);
			}
			$query->limit(($page - 1) * $page_size,$page_size);
		});
		if(count($data)<1){
			$data=[];
		}

		$res_Data=[];
		if(count($data) < $page_size){
			$no_ids=array_column($data, 'id');
			$res_Data=self::getRandListsArray($ap_id,$no_ids,$page,$page_size - count($data));

		}
		$data=array_merge($data,$res_Data);



		return $data;
	}

	/**
	 * 获取随机列表
	 */
	public static function getRandListsArray($ap_id,$no_ids,$page,$page_size)
	{
		$data = self::all(function($query) use($ap_id,$no_ids,$page,$page_size){
			$time = time();
			$query->field('id,link,url,target_type,content,link_id');
			$query->where('position_id',$ap_id);
			$query->where('enabled',1);
			$query->where('start_time','<',$time);
			$query->where('end_time','>',$time);
			$query->order('orderby asc,id desc');
			if($no_ids){
				$query->where('id','not in',$no_ids);
			}
			$query->limit(($page - 1) * $page_size,$page_size);
		});
		return $data;
	}

	public function getUrlAttr($value,$data)
	{
		return wjb_get_image_url($value);
	}
}
